package com.example.service.impl;

import com.example.Repository.BlogRepository;
import com.example.domain.Blog;
import com.example.domain.property.Tag;
import com.example.domain.property.Type;
import com.example.service.BlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSetPersister;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogRepository blogRepository;
    //新增和修改
    @Override
    public boolean insert(Blog blog) {
        if(blog.getCreateTime()==null){   //新增的时候
            blog.setCreateTime(new Date());
            blog.setViews(0);
        }
        blog.setUpdateTime(new Date());
        System.out.println("处理tagList集合后的blog:"+blog);
        Blog save = blogRepository.save(blog);
        if(blog.getId()==save.getId()){
            return true;
        }
        return false;
    }
    //查询一个
    @Override
    public Blog selectOne(Long id) {
        Optional<Blog> optional = blogRepository.findById(id);
        Blog blog=optional.get();
        return blog;
    }

    //分页查询多个
    @Override
    public Page<Blog> selectBlogPage(Pageable pageable, Blog blog) {
        Page<Blog> page = blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates=new ArrayList<>();
                if(!"".equals(blog.getTitle())&&blog.getTitle()!=null){
                    predicates.add(cb.like(root.<String>get("title"),"%"+blog.getTitle()+"%"));
                }
                if(blog.getType()!=null&&blog.getType().getId()!=null){
                    predicates.add(cb.equal(root.<Type>get("type").get("id"),blog.getType().getId()));
                }
                if(blog.isRecommend()){
                    predicates.add(cb.equal(root.<Boolean>get("recommend"),blog.isRecommend()));
                }
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
        return page;
    }
    //id删除
    @Override
    public boolean deleteById(Long id) {
        int i = blogRepository.DeteleBlogId(id);
        return i>0;
    }
    //根据id查询
    @Override
    public Blog selectOneId(Long id) {
        Optional<Blog> optional = blogRepository.findById(id);
        Blog blog=optional.get();
        return blog;
    }
    //根据推荐是否更新时间排序分页查询
    @Override
    public Page<Blog> gainBlogPage(Pageable pageable, Blog blog) {
        Page<Blog> page = blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates=new ArrayList<>();
                if(blog.isRecommend()){
                    predicates.add(cb.equal(root.<Boolean>get("recommend"),blog.isRecommend()));
                }
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
        return page;
    }
    //归档页时间查询
    @Override
    public List<Blog> findAllByCreateTimeLike() {
        List<Blog> blogList = blogRepository.findAll();
        return blogList;
    }
    //全文搜索方法
    @Override
    public Page<Blog> findByQuery(String query,int currentPage,int pageSize) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "updateTime");  //按更新时间降序排序
        Sort sort = Sort.by(order);
        Pageable pageable=PageRequest.of(currentPage,pageSize,sort);
        Page<Blog> page =blogRepository.findByQuery("%" + query + "%", pageable);
        return page;
    }

}
